<template>
  <j-form-container :disabled="disabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="商品编号id，外键" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['goodsId']" placeholder="请输入商品编号id，外键" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="商品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['goodsName']" placeholder="请输入商品名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="商品型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['goodsModel']" placeholder="请输入商品型号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="销售数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['goodsNum']" placeholder="请输入销售数量" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="商品单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['goodsUnit']" placeholder="请输入商品单位"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="商品单价" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['price', validatorRules.price]" placeholder="请输入商品单价" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="总金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['total', validatorRules.total]" placeholder="请输入总金额" style="width: 100%"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
  </j-form-container>
</template>
<script>
  import pick from 'lodash.pick'
  import { getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'

  export default {
    name: 'SaleListGoodsForm',
    components: {
      JFormContainer,
    },
    props:{
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        validatorRules: {
          price: {
            rules: [
              { required: true, message: '请输入商品单价!'},
            ]
          },
          total: {
            rules: [
              { required: true, message: '请输入总金额!'},
            ]
          },
        },
        confirmLoading: false,
      }
    },
    methods:{
      initFormData(url,id){
        this.clearFormData()
        if(!id){
          this.edit({})
        }else{
          getAction(url,{id:id}).then(res=>{
            if(res.success){
              let records = res.result
              if(records && records.length>0){
                this.edit(records[0])
              }
            }
          })
        }
      },
      edit(record){
        this.model = Object.assign({}, record)
        console.log("SaleListGoodsForm-edit",this.model);
        let fieldval = pick(this.model,'goodsId','goodsName','goodsModel','goodsNum','goodsUnit','price','total')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
      },
      getFormData(){
        let formdata_arr = []
        this.form.validateFields((err, values) => {
          if (!err) {
            let formdata = Object.assign(this.model, values)
            let isNullObj = true
            Object.keys(formdata).forEach(key=>{
              if(formdata[key]){
                isNullObj = false
              }
            })
            if(!isNullObj){
              formdata_arr.push(formdata)
            }
          }else{
            this.$emit("validateError","tb_sale_list_goods表单校验未通过");
          }
        })
        console.log("tb_sale_list_goods表单数据集",formdata_arr);
        return formdata_arr;
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'goodsId','goodsName','goodsModel','goodsNum','goodsUnit','price','total'))
      },
      clearFormData(){
        this.form.resetFields()
        this.model={}
      },
    }
  }
</script>
